package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.collector.Collector;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class Ku6Task  extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_KU6;// 对应网站ID
	private static final String ENCoding = TOP10Global.Encoding_GDK;// 对应网站ID

	public Ku6Task() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new MoiveParser());
		PARSER.add(new PlayTopParser());
	}

	public static void main(String args[]) throws Exception {
		new MoiveParser().dealText(ENCoding);
	}
}

abstract class Ku6Parser extends TOPParserImpl {
	
	public Ku6Parser(String c,String url) {
		super(c,TOP10Global.TYPE_Television, url);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract Video build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	

}
class PlayTopParser extends Ku6Parser{
	private final static String WEBURL="http://top.ku6.com/";
	private final static String SIGN="boke_list spin_list cfix";
	private final static String TITLE="最多播放";
	public PlayTopParser() {
		super(TITLE,WEBURL);
	}

	public Video build(Node a, int rank) throws Exception {
		
		String url = getAttrValueByTag(a, "href");
		if(url.indexOf("com/playlist")!=-1&&a.getTextContent()!=null&&!a.getTextContent().trim().equals("")){
			Video v = new Video();
			v.setRank(rank);
			v.setPlayUrl(url);
			v.setName(a.getTextContent());
			return v;
		}
		return null;
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div", "class",SIGN, body);
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<Video> list = new ArrayList<Video>();
			int rank = 1;
			for (Node d : dls) {
				Video v = build(d, rank);
				if(v!=null){
					list.add(v);
					System.out.println(v.toString());
					rank++;
					if(rank>10)break;
				}
			}
			return list;
		}
		return null;
	}
}

class MoiveParser extends Ku6Parser{
	private final static String WEBURL="http://www.juchang.com/toplist.htm?t=toplistPage&cid=1";
	private final static String SIGN="ulnrlb_q ulClear";
	private final static String TITLE="电影排行";
	public MoiveParser() {
		super(TITLE, WEBURL);
	}

	public Video build(Node a, int rank) throws Exception {
		String url = getAttrValueByTag(a, "href");
		if(url.indexOf("com/film/")!=-1&&!a.getTextContent().trim().equals("进入")){
			Video v = new Video();
			v.setRank(rank);
			v.setPlayUrl(url);
			v.setName(a.getTextContent());
			return v;
		}
		return null;
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("ul", "class",SIGN, body);
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<Video> list = new ArrayList<Video>();
			int rank = 1;
			for (Node d : dls) {
				Video v = build(d, rank);
				if(v!=null){
					list.add(v);
					System.out.println(v.toString());
					rank++;
					if(rank>10)break;
				}
			}
			return list;
		}
		return null;
	}
	
}